local skel = fk.CreateSkill {
  name = "lb_zero__shixi",
}

Fk:loadTranslationTable {
  ["lb_zero__shixi"] = "释昔",
  [":lb_zero__shixi"] = "当你参与过伤害结算的回合结束时，你可以摸一张牌并使用一张与你使用过的牌名相同的牌，然后视为未使用过此牌并摸一张牌。",

  ["#lb_zero__shixi"] = "释昔：你可以摸两张牌并使用一张与你使用过的牌名相同的牌，然后视为未使用过此牌",
  ["#lb_zero__shixi-use"] = "释昔：请使用一张与你使用过的牌名相同的牌",


  ["$lb_zero__shixi1"] = "即刻，大刑伺候！",
  ["$lb_zero__shixi2"] = "认罪伏法吧。",
}

skel:addEffect(fk.TurnEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and #player.room.logic:getEventsOfScope(GameEvent.Damage, 1, function(e)
      local damage = e.data
      return (damage.from == player or damage.to == player) and not damage.prevented
    end, Player.HistoryTurn) == 1
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__shixi" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, skel.name)
    if player:isAlive() then
      local list = {}
      for name, scope in pairs(player.cardUsedHistory) do
        for _, spe in ipairs(scope) do
          if spe > 0 then
            table.insert(list, name)
            break
          end
        end
      end
      if #list > 0 then
        local use = room:askToUseRealCard(player, {
          pattern = table.concat(list, ","),
          skill_name = skel.name,
          prompt = "#lb_zero__shixi-use",
          cancelable = false,
          skip = true,
        })
        if use then
          local name = use.card.trueName
          room:useCard(use)
          for _, scope in ipairs({ Player.HistoryPhase, Player.HistoryTurn, Player.HistoryRound, Player.HistoryGame }) do
            player:setCardUseHistory(name, 0, scope)
          end
          local mark = player:getTableMark("@[lb_zero__luoqian]lb_zero__luoqian")
          local all_name = mark.all_name or {}
          if table.removeOne(all_name, name) then
            local combo = mark.combo or {}
            for i = #combo, 1, -1 do
              local isUse = table.contains(all_name, combo[i][1])
              local state = isUse and "yin" or "yang"
              if player:getSwitchSkillState("lb_zero__luoqian", false, true) == state then
                state = "log_" .. state
              end
              combo[i][2] = state
            end
            room:setPlayerMark(player, "@[lb_zero__luoqian]lb_zero__luoqian", { all_name = all_name, combo = combo })
          end
          if player:isAlive() then
            player:drawCards(1, skel.name)
          end
        end
      end
    end
  end,
})


return skel
